﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECCIT;
using ECCIT.Utilities;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using System.Data;
using OgilvyOne.Utility;
using OgilvyOne.UKNow.Configuration;
using System.IO;

public partial class GateService_MyAccountAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // if (!IsPostBack)
        {
            var requestCmd = Request["cmd"] ?? string.Empty;
            var langIDStr = Request["langID"] ?? string.Empty;
            int lanagerid = 2052; //1033
            if (!string.IsNullOrEmpty(langIDStr))
            {
                if (int.TryParse(langIDStr, out lanagerid))
                {
                }
            }
            if(new AccountController(lanagerid).AccountInfo==null)
            {                
                return;
                
            }
            int? memberid = new AccountController(lanagerid).AccountInfo.ID;
            if (memberid == null) return;
            ResultInfo res=new ResultInfo();

            if (!string.IsNullOrEmpty(requestCmd))
            {
                switch (requestCmd)
                {
                    case "MyCommentlist":
                        int pageSise = Request.QueryString["pageSise"] != null
                                           ? int.Parse(Request.QueryString["pageSise"])
                                           : 3;
                        int pageIndex = Request.QueryString["pageIndex"] != null
                                            ? int.Parse(Request.QueryString["pageIndex"])
                                            : 1;
                        PagingList list = new EventsCommentBLL(lanagerid).GetEventCommentsByMemberID(memberid, pageIndex,
                                                                                                     pageSise);
                        //var ss = JsonUtil.Serialize(list);
                        ResponseJson(JsonUtil.Serialize(list));  
                        break;
                    case "DelMyComment":
                        var commentid = Request.QueryString["commentid"] != null
                                            ? int.Parse(Request.QueryString["commentid"])
                                            : 0;
                        if (commentid > 0)
                        {
                             res =
                                new EventsCommentBLL(lanagerid).Update(new EventsCommentModel()
                                                                           {
                                                                               LanguageID = lanagerid,
                                                                               Content =
                                                                                   Common.GetCommentDelContent(lanagerid),
                                                                               ID = commentid,
                                                                               MemberID = memberid
                                                                           });
                            EventsCommentModel model=new EventsCommentBLL(lanagerid).selEvent(commentid);
                            string res1="";
                            if (!string.IsNullOrEmpty(model.ImageName))
                                FileUtil.Delete(Path.Combine(ConfigCacheManager.FilePath.UploadFrontImagePath.PhysicalPath, model.ImageName), ref res1);
                            if (!string.IsNullOrEmpty(model.VideoName))
                                FileUtil.Delete(Path.Combine(ConfigCacheManager.FilePath.UploadFrontMediaPath.PhysicalPath, model.VideoName), ref res1);

                             ResponseJson(JsonUtil.Serialize(res));
                        }
                        break;
                    case "MyFavoriteEvents":
                        IList<FavoriteEventsModel> listModel = new EventsBLL(lanagerid).GetMyEvents((int)memberid, "", "", true);
                        //DataTable dt = new EventsBLL(lanagerid).GetMyEvents((int) memberid, "", "", true);

                        
                        if (listModel != null && listModel.Count > 0)
                        {
                            //var mm = JsonUtil.Serialize(listModel);
                            ResponseJson(JsonUtil.Serialize(listModel));
                        }
                        break;
                    case "MyAccount":
                        DataTable dt = new MemberBLL(lanagerid).GetMyAccount((int)memberid);
                        dt.Columns.Add("Interests", typeof(System.String));
                        dt.Rows[0]["Interests"] = new MemberBLL(lanagerid).GetMyInterestStr((int)memberid);  
                        string content = OgilvyOne.UKNow.Utility.DataTableExtensions.GetJSONString(dt);
                        ResponseJson(content);
                        break;
                    case "MyProfile":
                        DataTable dataTable = new MemberBLL(lanagerid).GetMyAccount((int) memberid);
                        dataTable.Columns.Add("Interests", typeof (System.String));
                        dataTable.Rows[0]["Interests"] = new MemberBLL(lanagerid).GetMyInterestStr((int) memberid);              
                        
                        ResponseJson(OgilvyOne.UKNow.Utility.DataTableExtensions.GetJSONString(dataTable));
                        break;
                    case "ReminderAll":
                        var eventbool = new EventsBLL(lanagerid).CheckMyEvents((int)memberid);
                        if (!eventbool)
                        {
                            ResponseState(false, "1");
                        }
                        else
                        {
                            var flag = new RemberSetBLL(lanagerid).RemindarAll((int) memberid);
                            if(flag)
                            {
                                ResponseState(true);
                            }
                            else
                            {
                                ResponseState(false, "2");

                            }
                        }
                       // ResponseState(eventbool && new RemberSetBLL(lanagerid).RemindarAll((int) memberid));
                        break;
                    case "ClearReminder":
                        var eventbool1 = new EventsBLL(lanagerid).CheckMyEvents((int)memberid);
                        if (!eventbool1)
                        {
                            ResponseState(false, "1");
                        }
                        else
                        {
                            MemberReminderMappingModel model=new MemberReminderMappingModel();
                            model.COL_LANG_ID = lanagerid;
                            model.MEMBER_ID = (int)memberid;
                            //清除所有收藏
                            new MemberReminderBLL(lanagerid).ClearAll(model);
                            ResponseState(new RemberSetBLL(lanagerid).ClearRemindar((int)memberid));
                        }                        
                        break;
                    case "LastEvents":
                        IList<FavoriteEventsModel> lastEventsModel = new EventsBLL(lanagerid).GetMyEvents(
                            (int) memberid, DateTime.Now.ToString(), "", false);
                        if (lastEventsModel != null && lastEventsModel.Count>0)
                        {
                            var con = JsonUtil.Serialize(lastEventsModel
                                );
                            ResponseJson(JsonUtil.Serialize(lastEventsModel
                                ));
                        }
                        break;
                    case "RemindEevent":
                        var eventid = Request.QueryString["eventid"] != null
                                          ? int.Parse(Request.QueryString["eventid"])
                                          : 0;
                        var optype = Request.QueryString["optype"] != null
                                         ? int.Parse(Request.QueryString["optype"])
                                         : -1;
                        if(eventid>0)
                        {
                            MemberReminderMappingModel Remindermodel = new MemberReminderMappingModel
                                                                   {
                                                                       MEMBER_ID = (int)memberid,
                                                                       COL_LANG_ID = lanagerid,
                                                                       COL_EVENTS_ID = eventid
                                                                   };
                            switch (optype)
                            {
                                case 1:
                                    {
                                        //增加
                                        ResultInfo result = new MemberReminderBLL(lanagerid).Save(Remindermodel);
                                        if (result.Result)
                                        {
                                            ResponseJson(JsonUtil.Serialize(result));
                                        }
                                    }
                                    break;
                                case 0:
                                    {
                                        //取消
                                        ResultInfo result = new MemberReminderBLL(lanagerid).Delete(Remindermodel);
                                        if (result.Result)
                                        {
                                            ResponseJson(JsonUtil.Serialize(result));
                                        }
                                    }
                                    break;
                            }
                        }

                        break;
                    case "GetMyInterests":
                        DataTable interestsdt = new ArtFormBLL(lanagerid).GetModifyMyProfile((int) memberid, lanagerid);
                        if (interestsdt != null && interestsdt.Rows.Count > 0)
                        {
                            ResponseJson(OgilvyOne.UKNow.Utility.DataTableExtensions.GetJSONString(interestsdt));
                        }
                        break;
                    case "GetMyProfile":
                        MemberModel memberModel= new MemberBLL(lanagerid).getMemberByID(memberid);                        
                        if (memberModel!=null)
                        {
                            memberModel.CheckPw = !string.IsNullOrEmpty(memberModel.Password);
                            var tt = JsonUtil.Serialize(memberModel);
                            ResponseJson(JsonUtil.Serialize(memberModel));
                        }
                        break;
                    case "CheckEmail":
                        var email = Request.QueryString["email"] ?? "";
                        if(email.Length>0)
                        {
                            ResponseState(!new MemberBLL(lanagerid).CheckEmail(email, (int) memberid));
                        }                 
                        break;
                    case "GetRemindarSet":
                        MemberReminderSettingModel memberReminderSettingModel =
                            new RemberSetBLL(lanagerid).GetModal((int) memberid);
                        if(memberReminderSettingModel!=null)
                        {
                            ResponseJson(JsonUtil.Serialize(memberReminderSettingModel));
                        }
                        break;
                    case "SaveRemSet":
                        var chk_email = Request.Form["chk_emailset"];
                        var chk_mobile = Request.Form["chk_mobileset"];
                        var txt_email = Request.Form["txt_emailset"];
                        var txt_mibile = Request.Form["txt_mobileset"];
                        MemberReminderSettingModel reminderSettingModel=new MemberReminderSettingModel();
                        reminderSettingModel.COL_MEMBER_ID = (int)memberid;
                        reminderSettingModel.COL_REM_BY_EMAIL = chk_email != null;
                        reminderSettingModel.COL_REM_BY_MOBILE = chk_mobile != null;
                        reminderSettingModel.COL_REM_EMAIL = txt_email;
                        reminderSettingModel.COL_REM_MOBILE = txt_mibile;
                        ResponseState(new RemberSetBLL(lanagerid).Set(reminderSettingModel));
                        break;
                    case "CancleMemberShip":
                        ResultInfo Cancelresult = new MemberBLL(lanagerid).CancelMember(
                            CommonConst.DeleteEventCommentCn, CommonConst.DeleteEventCommentEn, (int) memberid);
                        //清用户的session
                        new AccountController(lanagerid).ProcessLogout();
                        ResponseJson(JsonUtil.Serialize(Cancelresult));
                        break;
                    case "delcommentimg"://清除用户上传的评论图片
                         string imgName = Request.Params["imgName"];
                         var bll = new WarmupBLL(lanagerid);
                        ResultInfo  cResultInfo = bll.DeleteCommentUploadedImage(imgName);
                        ResponseJson(JsonUtil.Serialize(cResultInfo));
                       // output = JsonUtil.Serialize(cResultInfo);
                        
                        break;
                    case "CheckUsername"://验证用户名是否存在
                        string username = Request.QueryString["username"];
                        if (!string.IsNullOrEmpty(username))
                        {
                            ResponseState(
                                !new MemberBLL(lanagerid).CheckUser(new AccountController(lanagerid).AccountInfo.Name,
                                                                   username));
                        }
                       
                        break;

                }

            }
        }
    }
    private void ResponseState(bool flag)
    {
        ResultInfo res = new ResultInfo();
        res.Result = flag;
        res.Message = flag ? "Success" : "false";
        Response.ContentType = "text/plain";
        Response.Write(JsonUtil.Serialize(res));
    }
    private void ResponseState(bool flag,string message)
    {
        ResultInfo res = new ResultInfo();
        res.Result = flag;
        res.Message = message;
        var tt = JsonUtil.Serialize(res);
        Response.ContentType = "text/plain";
        Response.Write(JsonUtil.Serialize(res));
    }

    private void ResponseJson(string content)
    {
        Response.ContentType = "text/plain";
        Response.Write(content);
    }

}